// 将时间戳转换为时间
export function timestampToTime(timestamp) {
	let newTimestamp = parseInt(timestamp);
	let date = new Date(newTimestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
	let Y = date.getFullYear() + "-";
	let M =
		date.getMonth() + 1 < 10
			? "0" + (date.getMonth() + 1) + "-"
			: date.getMonth() + 1 + "-";
	let D =
		date.getDate() < 10 ? "0" + date.getDate() + " " : date.getDate() + " ";
	let h =
		date.getHours() < 10 ? "0" + date.getHours() + ":" : date.getHours() + ":";
	let m =
		date.getMinutes() < 10
			? "0" + date.getMinutes() + ":"
			: date.getMinutes() + ":";
	let s =
		date.getSeconds() < 10
			? "0" + date.getSeconds() + ""
			: date.getSeconds() + "";
	return Y + M + D + h + m + s;
}

// 获取每月1号
export function getYearMonthDayFromOne() {
	let date = new Date();
	let resDate = date.getFullYear() + '-' + (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) + "-" : date.getMonth() + 1 + "-") + '01';
	return resDate;
}

// 获取当天
export function getYearMonthDay() {
	let date = new Date();
	let resDate = date.getFullYear() + '-' + (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) + "-" : date.getMonth() + 1 + "-") + (date.getDate() < 10 ? "0" + date.getDate() : date.getDate());
	return resDate;
}

// 获取昨天
export function getLastDay() {
	let day1 = new Date();
	day1.setTime(day1.getTime() - 24 * 60 * 60 * 1000);
	let s1 = day1.getFullYear() + "-" + (day1.getMonth() + 1 < 10 ? "0" + (day1.getMonth() + 1) + "-" : day1.getMonth() + 1 + "-") + (day1.getDate() < 10 ? "0" + day1.getDate() : day1.getDate());
	return s1;
}

// 获取从day1到day2的所有日期
export function getFromDay1ToDay2(day1, day2) {
	var getDate = function (str) {
		var tempDate = new Date();
		var list = str.split("-");
		tempDate.setFullYear(list[0]);
		tempDate.setMonth(list[1] - 1);
		tempDate.setDate(list[2]);
		return tempDate;
	}
	var date1 = getDate(day1);
	var date2 = getDate(day2);
	if (date1 > date2) {
		var tempDate = date1;
		date1 = date2;
		date2 = tempDate;
	}
	date1.setDate(date1.getDate() + 1);
	var dateArr = [];
	var i = 0;
	while (!(date1.getFullYear() == date2.getFullYear()
		&& date1.getMonth() == date2.getMonth() && date1.getDate() == date2
			.getDate())) {
		var dayStr = date1.getDate().toString();
		if (dayStr.length == 1) {
			dayStr = "0" + dayStr;
		}
		var monthStr = (date1.getMonth() + 1).toString();
		if (monthStr.length == 1) {
			monthStr = "0" + monthStr;
		}
		dateArr[i] = date1.getFullYear() + "-" + monthStr + "-"
			+ dayStr;
		i++;
		/* 
		 * document.write("<div style='display:block'>" + date1.getFullYear() + 
		 * "-" + (date1.getMonth() + 1) + "-" + date1.getDate() + "</div>"); 
		 */
		// document.write(dateArr[i] + "<br>");  
		date1.setDate(date1.getDate() + 1);
	}
	dateArr.splice(0, 0, day1)
	dateArr.push(day2);
	return dateArr;
}